package com.example.demo6.model;

import java.sql.*;

public class test1 {
    String name;
    String pass;
    public void userLogin(String name,String pass){
        this.name=name;
        this.pass=pass;
    }

    public String selecter() {
        String url = "jdbc:mysql://localhost:3306/library_management?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
        String username = "root"; // 你的数据库用户名
        String password = "a20060713"; // 你的数据库密码

        Connection conn=null;
        PreparedStatement prep=null;
        ResultSet rs=null;
        String namechecked=null;

        String sql="select * from user where username = ? and password = ?";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url,username,password);

            prep = conn.prepareStatement(sql);
            prep.setString(1,name);
            prep.setString(2,pass);

            rs = prep.executeQuery();

            while (rs.next()){
                namechecked=rs.getString("name");
            }

            if (namechecked!=null){
                return namechecked;
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                prep.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
